﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Data;
using System.Data.Common;
using System.Diagnostics;

namespace MySQL.Db
{
	public class ResultSet
	{
		ResultSetColumnCollection columns;

		internal ResultSet()
		{
			this.columns = new ResultSetColumnCollection();
		}

		internal ResultSet(DbDataReader reader)
			: this()
		{
			this.columns.Populate(reader);
		}

		public bool GetBool(int row, int column) { return this.columns[column].GetBool(row); }
		public byte GetByte(int row, int column) { return this.columns[column].GetByte(row); }
		public char GetChar(int row, int column) { return this.columns[column].GetChar(row); }
		public DateTime GetDateTime(int row, int column) { return this.columns[column].GetDateTime(row); }
		public decimal GetDecimal(int row, int column) { return this.columns[column].GetDecimal(row); }
		public double GetDouble(int row, int column) { return this.columns[column].GetDouble(row); }
		public float GetFloat(int row, int column) { return this.columns[column].GetFloat(row); }
		public Int16 GetInt16(int row, int column) { return this.columns[column].GetInt16(row); }
		public Int32 GetInt32(int row, int column) { return this.columns[column].GetInt32(row); }
		public Int64 GetInt64(int row, int column) { return this.columns[column].GetInt64(row); }
		public UInt16 GetUInt16(int row, int column) { return this.columns[column].GetUInt16(row); }
		public UInt32 GetUInt32(int row, int column) { return this.columns[column].GetUInt32(row); }
		public UInt64 GetUInt64(int row, int column) { return this.columns[column].GetUInt64(row); }
		public string GetString(int row, int column) { return this.columns[column].GetString(row); }
		public sbyte GetSByte(int row, int column) { return this.columns[column].GetSByte(row); }
		public object GetObject(int row, int column) { return this.columns[column].GetObject(row); }

		public bool GetBool(int row, string column) { return this.columns[column].GetBool(row); }
		public byte GetByte(int row, string column) { return this.columns[column].GetByte(row); }
		public char GetChar(int row, string column) { return this.columns[column].GetChar(row); }
		public DateTime GetDateTime(int row, string column) { return this.columns[column].GetDateTime(row); }
		public decimal GetDecimal(int row, string column) { return this.columns[column].GetDecimal(row); }
		public double GetDouble(int row, string column) { return this.columns[column].GetDouble(row); }
		public float GetFloat(int row, string column) { return this.columns[column].GetFloat(row); }
		public Int16 GetInt16(int row, string column) { return this.columns[column].GetInt16(row); }
		public Int32 GetInt32(int row, string column) { return this.columns[column].GetInt32(row); }
		public Int64 GetInt64(int row, string column) { return this.columns[column].GetInt64(row); }
		public UInt16 GetUInt16(int row, string column) { return this.columns[column].GetUInt16(row); }
		public UInt32 GetUInt32(int row, string column) { return this.columns[column].GetUInt32(row); }
		public UInt64 GetUInt64(int row, string column) { return this.columns[column].GetUInt64(row); }
		public string GetString(int row, string column) { return this.columns[column].GetString(row); }
		public sbyte GetSByte(int row, string column) { return this.columns[column].GetSByte(row); }
		public object GetObject(int row, string column) { return this.columns[column].GetObject(row); }

		public int Count
		{
			get
			{
				if (this.columns.Count > 0)
				{
					return this.columns[0].Count;
				}

				return 0;
			}
		}

		public XDocument ToXml()
		{
			XElement root = new XElement("root");
			XDocument doc = new XDocument(root);

			if (this.Count > 0)
			{
				for (int i = 0; i < this.Count; i++)
				{
					XElement record = new XElement("record", new XAttribute("number", (i + 1).ToString()));
					for (int j = 0; j < this.columns.Count; j++)
					{
						record.Add(new XElement("field", new XAttribute("name", this.columns[j].Name), new XAttribute("value", this.GetObject(i, j) ?? "NULL")));
					}

					root.Add(record);
				}
			}
			return doc;
		}
	}
}
